// nbmade-vue3/src/api/iot/nodered.ts
import request from '@/utils/request'

// Node-RED Flow 管理
export function getNodeRedFlows(params: any) {
  return request({ url: '/iot/nodered/flows', method: 'get', params })
}

export function getNodeRedFlow(id: string | number) {
  return request({ url: `/iot/nodered/flows/${id}`, method: 'get' })
}

export function createNodeRedFlow(data: any) {
  return request({ url: '/iot/nodered/flows', method: 'post', data })
}

export function updateNodeRedFlow(data: any) {
  return request({ url: '/iot/nodered/flows', method: 'put', data })
}

export function deleteNodeRedFlow(id: number) {
  return request({ url: `/iot/nodered/flows/${id}`, method: 'delete' })
}

export function deployFlow(id: number) {
  return request({ url: `/iot/nodered/flows/${id}/deploy`, method: 'post' })
}

export function stopFlow(id: number) {
  return request({ url: `/iot/nodered/flows/${id}/stop`, method: 'post' })
}

export function getFlowStatus(id: number) {
  return request({ url: `/iot/nodered/flows/${id}/status`, method: 'get' })
}

export function importFlow(flowJson: string) {
  return request({ url: '/iot/nodered/flows/import', method: 'post', data: flowJson })
}

export function exportFlow(id: number) {
  return request({ url: `/iot/nodered/flows/${id}/export`, method: 'get' })
}

export function getEditorUrl() {
  return request({ url: '/iot/nodered/editor', method: 'get' })
}

// 规则节点管理
export function getRuleNodes(params: any) {
  return request({ url: '/iot/rule-nodes/page', method: 'get', params })
}

export function getRuleNode(id: number) {
  return request({ url: `/iot/rule-nodes/${id}`, method: 'get' })
}

export function createRuleNode(data: any) {
  return request({ url: '/iot/rule-nodes', method: 'post', data })
}

export function updateRuleNode(data: any) {
  return request({ url: '/iot/rule-nodes', method: 'put', data })
}

export function deleteRuleNode(id: number) {
  return request({ url: `/iot/rule-nodes/${id}`, method: 'delete' })
}

export function getNodeTypes() {
  return request({ url: '/iot/rule-nodes/types', method: 'get' })
}

// 获取节点模板
export function getNodeTemplates() {
  return request({ url: '/iot/nodered/templates', method: 'get' })
}

// 验证Flow配置
export function validateFlow(flowConfig: string) {
  return request({ url: '/iot/nodered/validate', method: 'post', data: flowConfig })
}